package cn.edu.nit.sys.mapper;

import cn.edu.nit.sys.entity.Subject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xiaobai
 * @Date 2023/3/19 15:02
 * @Version 1.0.0
 */
@Mapper
public interface SubjectMapper extends BaseMapper<Subject> {
    Page<Subject> getSubjectList(Page<Subject> page, String siftClazz, String siftName, String siftId);

    @Select("select id, name, teacher_id, teacher, clazz, duration, time as timeStr, grade from sys_subject where year = #{year}")
    List<Subject> getAllSubject(String year);

    @Insert("insert into sys_sign " +
            "values (#{id}, #{name}, #{subjectId}, #{type}, #{code}, #{position}, #{duration}, #{curDate}, #{endDate})")
    int addSign(String id, String name, String subjectId, String type,
                int duration, String position, String code, Date curDate, Date endDate);

    @Select("select id, name, subject_id, type, code, position, duration, create_time, end_time," +
            "(select count(id) from sys_sign_log where a.id = sign_id) as sign, " +
            "(select count(c.id) from sys_student c, sys_subject b " +
            "where b.id = a.subject_id and b.clazz rlike concat(c.major, c.clazz, '班')) as total " +
            "from sys_sign a " +
            "where subject_id = #{id} order by create_time desc")
    Page<Map<String, String>> getSignList(Page<Map<String, String>> page, String id);

    @Select("select d.avatar, c.id, c.name, b.id as uid, " +
            "if((select id from sys_leave " +
            "where user_id = c.uid and create_time < a.create_time " +
            "and DATE_ADD(create_time,interval day day) > a.create_time) is not null, true, false) as noSign " +
            "from sys_sign a " +
            "left join sys_subject b on a.subject_id = b.id " +
            "left join sys_student c on b.clazz rlike concat(c.major, c.clazz, '班') " +
            "left join sys_user d on c.uid = d.id " +
            "where a.id = #{id} and c.id not in (select c.id from sys_sign_log a " +
            "left join sys_user b on a.user_id = b.id " +
            "left join sys_student c on c.uid = b.id " +
            "where a.sign_id = #{id})")
    List<Map<String, String>> noSignList(String id);

    @Select("select c.id, c.name, b.avatar, a.status from sys_sign_log a " +
            "left join sys_user b on a.user_id = b.id  " +
            "left join sys_student c on c.uid = b.id  " +
            "where a.sign_id = #{id}")
    List<Map<String, String>> signList(String id);

    @Select("select c.id, c.name, b.name as subject, c.subject_id, c.type, " +
            "c.code, c.position, c.duration, c.create_time, date_format(c.end_time, '%Y-%m-%d %H:%i:%s') as end_time, " +
            "if(c.create_time <= now(), true, false) as timeout " +
            "from sys_student a " +
            "left join sys_subject b on  b.clazz rlike concat(a.major, a.clazz, '班') " +
            "left join sys_sign c on c.subject_id = b.id " +
            "where a.uid = #{id} and c.id is not null")
    Page<Map<String, String>> getSignListForStudent(Page<Map<String, String>> page, String id);

    @Select("select id from sys_sign where id = #{signId} and end_time > now()")
    String signIsExpired(String signId);

    @Insert("insert into sys_sign_log " +
            "values (#{id}, #{signId}, #{uid}, #{position}, #{img}, #{status}, now(), #{remark})")
    int addSignLog(String id, String signId, String uid, String img, String position, String status, String remark);

    @Select("select id from sys_sign_log where sign_id = #{id} and user_id = #{uid}")
    String isSigned(String id, String uid);

    @MapKey("id")
    Page<Map<String, Object>> getSignRecord(Page<Map<String, String>> page, String siftName, String id);
}
